﻿#include "Thread.h"
#include "IRunable.h"
#include "Mutex.h"
#include <iostream>
#include <vector>
#include <string>

using namespace std;
using namespace sky;

// #pragma comment(lib, "Base.dll")

class ProgressBar: ThreadRunnable
{

public:
    ProgressBar();
    ~ProgressBar();
    
    virtual void run();
    virtual void release();

public:
    void start(int ms = 0xFFFFFFFF);
    void stop();

    void terminate();
    
private:
    string m_chs;
    Semaphore m_Semaphore;
    Thread m_thread;
};



int main()
{
    ProgressBar pb;
    pb.start(3000);
    pb.stop();

    return 0;
}

ProgressBar::ProgressBar()
{
    m_chs = "\\|/-";
    
}

ProgressBar::~ProgressBar()
{
    m_thread.terminate();
}

void ProgressBar::run()
{

    size_t len = m_chs.size();

    for (int i=0; i < len; )
    {
        cout << "\r" << m_chs[i] << " loading ......";
        if (m_Semaphore.Wait(100))
            break;

        i = (++i) % len;
    }

    cout << "\r";

    string strBlank(strlen("\r loading ......"), ' ');

    cout << strBlank.c_str();

    cout << "\r";
}

void ProgressBar::release()
{
    m_thread.terminate();
    delete this;
}

void ProgressBar::start(int ms)
{
    m_thread.spawn((ThreadRunnable *)this);

    m_thread.sleep(ms);
    
}


void ProgressBar::stop()
{
    m_Semaphore.Post();
}

void ProgressBar::terminate()
{
    m_thread.terminate();
}
